package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import jdk.nashorn.internal.scripts.JO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("sessions")
public class SessionController {
    @Autowired
    private IUserService userService;
    @PostMapping
    public JSONResult login(@RequestParam String email , @RequestParam String password){
        JSONResult jsonResult = new JSONResult();
        try {
            //判断用户的账号密码是否正确
            User user = userService.login(email, password);
            //登录成功后将user放在session中
            UserContext.setCurrentUser(user);
            //登录成功将当前用户信息返回
            jsonResult.setData(user);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
            jsonResult.setSuccess(false);
        }
        return jsonResult;
    }
    //注销操作
    @DeleteMapping
    public JSONResult logout(HttpSession session){
        //删除session
        session.invalidate();
        //返回到登录界面
        return new JSONResult();
    }
}
